/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package main;

/**
 *
 * @author ZSQ
 */
/**
 * 神经元类
 * 由基础组件与数据工程师实现
 */


public class Neuron {
    private double[] weights;
    private double bias;
    private double output;
    private double delta;
    private double[] lastInputs;
    
    public Neuron(int inputSize) {
        weights = new double[inputSize];
        // 使用Xavier/Glorot初始化，避免梯度消失
        double scale = Math.sqrt(2.0 / inputSize);
        bias = (Math.random() * 2 - 1) * scale * 0.1; // 更小的偏置

        for (int i = 0; i < inputSize; i++) {
            weights[i] = (Math.random() * 2 - 1) * scale;
        }
    }
    
    public double calculateOutput(double[] inputs, ActivationFunction activationFunction) {
        this.lastInputs = inputs;
        double sum = bias;
        
        for (int i = 0; i < inputs.length; i++) {
            sum += inputs[i] * weights[i];
        }
        
        output = activationFunction.calculate(sum);
        return output;
    }
    
    // Getter 和 Setter 方法
    public double[] getWeights() { return weights; }
    public void setWeights(double[] weights) { this.weights = weights; }
    public double getBias() { return bias; }
    public void setBias(double bias) { this.bias = bias; }
    public double getOutput() { return output; }
    public double getDelta() { return delta; }
    public void setDelta(double delta) { this.delta = delta; }
    public double[] getLastInputs() { return lastInputs; }
}